package com.ruoyi.shop.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.User;
import com.ruoyi.shop.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息管理Controller
 *
 * @author ruoyi
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/backstage/userinfo")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user) {

        startPage();
        List<User> list = userService.selectUserList(user);

        for (User u : list) {
            u.setPhoto("/profile/upload/" + u.getPhoto());
        }

        return getDataTable(list);
    }

    /**
     * 导出用户信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:export')")
    @Log(title = "用户信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息管理数据");
    }

    /**
     * 获取用户信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        User user = userService.selectUserById(id);
        user.setPhoto("/profile/upload/" + user.getPhoto());
        return AjaxResult.success(user);
    }

    /**
     * 新增用户信息管理
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:add')")
    @Log(title = "用户信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户信息管理
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:edit')")
    @Log(title = "用户信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        user.setPhoto(StrUtil.removePrefix(user.getPhoto(), "/profile/upload/"));

        int i = userService.updateUser(user);
        return toAjax(i);
    }

    /**
     * 逻辑删除用户
     */
    @PreAuthorize("@ss.hasPermi('backstage:userinfo:remove')")
    @Log(title = "用户信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 解除逻辑删除用户
     */
    @Log(title = "用户信息管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/alert/{ids}")
    public AjaxResult alert(@PathVariable Long[] ids) {
        return toAjax(userService.alertUserByIds(ids));
    }
}
